﻿using eShop.Core;
using eShop.Services;
using System;
using System.Web.Mvc;

namespace eShop.Web.Framework
{
    /// <summary>
    /// 基础控制器类
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 检查是否注册
        /// </summary>
        /// <returns></returns>
        private bool CheckRegCode(out string errMsg)
        {
            errMsg = "";
            //////1、获取本地机器码
            //string machineCode = BSPVersion.SHOP_MACHINECODE;//RSApublic.GetMachineCode();
            //////2、获取加密后的日期
            //string regCode = BSPVersion.SHOP_REGCODE;
            ////判断注册码 是否为空
            //if (string.IsNullOrWhiteSpace(regCode))
            //{
            //    errMsg = string.Format("注册码不能为空,无法继续使用，请联系管理员.特征码：\r\n{0}", RSApublic.longStringRSAEncry(machineCode));
            //    return false;
            //}
            //string[] matchArray = regCode.Split('|');
            //string datetimeStr = "";
            //if (matchArray.Length <= 1)
            //{
            //    datetimeStr = DateTime.Now.ToString("yyyyMMdd");
            //}
            //else
            //{
            //    datetimeStr = matchArray[1].ToString();
            //}

            //try
            //{
            //    string decodeDateTimeStr = ZEncypt.DESDecrypt(datetimeStr);
            //    //验证字符串 注册码 + 有效期 
            //    string validStr = machineCode + "|" + decodeDateTimeStr;
            //    string signStr = matchArray[0];
            //    if (RSApublic.varifySign(signStr, validStr))
            //    {
            //        //判断日期
            //        if (DateTime.Parse(decodeDateTimeStr) < DateTime.Now)
            //        {
            //            errMsg = string.Format("系统已过有效期{0},无法继续使用，请联系管理员.特征码：\r\n{1}", decodeDateTimeStr, RSApublic.longStringRSAEncry(machineCode));
            //            return false;
            //        }
            //        else
            //            return true;
            //    }
            //    else
            //    {
            //        errMsg = string.Format("注册码不正确，无法继续使用，请联系管理员.特征码：\r\n{0}", RSApublic.longStringRSAEncry(machineCode));
            //        return false;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    errMsg = ex.Message;
            //    return false;
            //}


            return true;
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string error = string.Empty;
            if (!CheckRegCode(out error))
            {
                //跳转的页面 2016-08-15
                ViewResult viewResult = new ViewResult()
                {
                    ViewName = "NoAccess"
                };
                viewResult.TempData.Add("Message", error);
                viewResult.TempData.Add("Title", "访问受限");
                filterContext.Result = viewResult;
                //filterContext.Result = new ContentResult() { Content = error };
            }
        }
        protected override void OnException(ExceptionContext filterContext)
        {
            Logs.Write(filterContext.Exception);
        }

        /// <summary>
        /// 获得路由中的值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        protected int GetRouteInt(string key, int defaultValue)
        {
            return TypeHelper.ObjectToInt(RouteData.Values[key], defaultValue);
        }

        /// <summary>
        /// 获得路由中的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        protected int GetRouteInt(string key)
        {
            return GetRouteInt(key, 0);
        }

        /// <summary>
        /// 获得路由中的值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        protected string GetRouteString(string key, string defaultValue)
        {
            object value = RouteData.Values[key];
            if (value != null)
                return value.ToString();
            else
                return defaultValue;
        }

        /// <summary>
        /// 获得路由中的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        protected string GetRouteString(string key)
        {
            return GetRouteString(key, "");
        }

        /// <summary>
        /// 提示信息视图
        /// </summary>
        /// <param name="backUrl">返回地址</param>
        /// <param name="message">提示信息</param>
        /// <returns></returns>
        protected ViewResult PromptView(string backUrl, string message)
        {
            return View("prompt", new PromptModel(backUrl, message));
        }

        /// <summary>
        /// 提示信息视图
        /// </summary>
        /// <param name="backUrl">返回地址</param>
        /// <param name="message">提示信息</param>
        /// <param name="isAutoBack">是否自动返回</param>
        /// <returns></returns>
        protected ViewResult PromptView(string backUrl, string message, bool isAutoBack)
        {
            return View("prompt", new PromptModel(backUrl, message, isAutoBack));
        }

        /// <summary>
        /// ajax请求结果
        /// </summary>
        /// <param name="state">状态</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        protected ActionResult AjaxResult(string state, string content)
        {
            return AjaxResult(state, content, false);
        }

        /// <summary>
        /// ajax请求结果
        /// </summary>
        /// <param name="state">状态</param>
        /// <param name="content">内容</param>
        /// <param name="isObject">是否为对象</param>
        /// <returns></returns>
        protected ActionResult AjaxResult(string state, string content, bool isObject)
        {
            return Content(string.Format("{0}\"state\":\"{1}\",\"content\":{2}{3}{4}{5}", "{", state, isObject ? "" : "\"", content, isObject ? "" : "\"", "}"));
        }
    }
}
